package edu.nf.ssm.service.impl;

import com.github.pagehelper.PageInfo;
import edu.nf.ssm.entity.City;
import edu.nf.ssm.mapper.CityMapper;
import edu.nf.ssm.service.CityService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author wangl
 * @date 2025/5/16
 */
@Service("cityService")
//事务注解（当前类中的方法都会参与到事务中）
//spring的事务是基于AOP实现的
//rollbackFor属性用于设置当遇到什么异常就进行回滚
//propagation属性用于设置事务的传播级别
@Transactional(rollbackFor = RuntimeException.class,
        propagation = Propagation.REQUIRED)
public class CityServiceImpl implements CityService {

    private CityMapper cityMapper;

    private UserService userService;

    public CityServiceImpl(UserService userService, CityMapper cityMapper) {
        this.userService = userService;
        this.cityMapper = cityMapper;
    }

    //只读事务，当设置为readOnly时，其他事务将不能参与写操作
    @Transactional(readOnly = true)
    @Override
    public PageInfo<City> listCity(int pageNum, int pageSize) {
        userService.findUser();
        List<City> cityList = cityMapper.listCity(pageNum, pageSize);
        return new PageInfo<>(cityList);
    }
}